<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改方法')" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-method-edit" th:object="${zknjMethod}">
        <input name="modId" th:field="*{modId}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">名称：</label>
            <div class="col-sm-8">
                <input name="modName" th:field="*{modName}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <input name="modDes" th:field="*{modDes}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="status" th:checked="${zknjMethod.isLock == 0}">
                    <span></span>
                </label>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">模板：</label>
            <div class="col-sm-8">
                <select id="template" name="templateName" class="form-control" th:field="*{templateName}">
                    <option th:each="template:${templates}" th:data="${template.temId}" th:value="${template.temName}" th:text="${template.temName}" th:disabled="${template.temStatus == '1'}" ></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">实验参数（部分）：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id="acqupara" class="form-control" type="text" placeholder="修改实验参数" readonly>
                    <span class="input-group-addon" title="恢复默认参数值" onmousedown="toAjax($('#template option:selected').attr('data'))"><i class="fa fa-refresh"></i></span>
                </div>
                <div class="ms-parent" style="width: 100%;">
                    <div class="acqupara-drop animated flipInX" style="max-height:200px;overflow-y:auto">

                    </div>
                </div>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 可修改实验参数在模板文件的autopara.xml中定义, 推荐使用默认值</span>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var prefix = ctx + "system/method";

    $("#form-method-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data = $('#form-method-edit').serializeArray();
            var param = [];
            $('.acqupara-drop').find("input").each(function (key, value, index) {
                var val = $(this).val();
                var name = $(this).attr("name");
                if($.common.isNotEmpty(name)){
                    param.push(name + "=" + val);
                }
            })
            var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
            data.push({"name": "modAcqupara", "value": param.join('|')});
            data.push({"name": "modStatus", "value": status});
            $.operate.save(prefix + "/edit", data);
        }
    }

    $(function () {
        var value = $("#template option:selected").attr("data");
        var params = [[${zknjMethod.modAcqupara}]];
        toAjax(value, params);
    });

    $("#template").change(function () {
        var temId = $("#template option:selected").attr("data");
        var temName = $("#template option:selected").val();
        var params = [[${zknjMethod.modAcqupara}]];
        var templateName = [[${zknjMethod.templateName}]];

        if ($.common.equals(temName, templateName)){
            toAjax(temId, params);
        }else {
            toAjax(temId);
        }
    })

    function toAjax(temId, ps){
        $.ajax({
            type: "post",
            url: prefix + "/selectTemplate",
            data: {
                "temId": temId
            },
            success: function(data) {
                $(".acqupara-drop").empty();
                if(data.code == web_status.SUCCESS){
                    var param = data.data.group.param;
                    var html = [];
                    var psData = new Map();
                    if($.common.isNotEmpty(ps)){
                        var params = ps.split("|");
                        for (let i = 0; i < params.length; i++) {
                            psData.set(params[i].split("=")[0], params[i].split("=")[1]);
                        }
                    }
                    $.each(param, function (key, value, index) {
                        var label = '<label class="col-sm-3 control-label">'+value.name+': </label>';
                        var type = 'text';
                        if (value.type == "real"){
                            //double
                            type = 'number';
                        }else if(value.type == "string"){
                            //string
                            type = 'text';
                        }else if(value.type == "integer"){
                            //int
                            type = 'number';
                        }else {
                            //other
                            type = 'text';
                        }
                        var input;
                        if($.common.isNotEmpty(ps) && $.common.isNotEmpty(psData.get(value.name))){
                            input = '<div class="col-sm-8"><input class="form-control" type="'+type+'" name="'+value.name+'" value="'+psData.get(value.name)+'" min="'+value.min+'" max="'+value.max+'"></div>';
                        }else {
                            input = '<div class="col-sm-8"><input class="form-control" type="'+type+'" name="'+value.name+'" value="'+value.value+'" min="'+value.min+'" max="'+value.max+'"></div>';
                        }
                        html.push(label);
                        html.push(input);
                    })
                    $(".acqupara-drop").append(html);
                }else {
                    top.layer.alert(data.msg, {
                        icon: $.modal.icon(modal_status.FAIL),
                        title: "错误提示",
                        btn: ['确认'],
                        btnclass: ['btn btn-primary'],
                    });
                }

            }
        });
    }
</script>
</body>
</html>